﻿using DBUtil;
using Function;
using Model;
using Model.Permission;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace DAL
{
   public class ST_GetCoinApplyDAL
    {
       DbHelperSQLP DbHelperSQL = new DbHelperSQLP(PubConstant.JHShop);
       public ST_GetCoinApplyDAL()
		{}
		#region  BasicMethod
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from ST_GetCoinApply");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(ST_GetCoinApply model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into ST_GetCoinApply(");
			strSql.Append("Code,InvestorID,BankID,BankCode,Owner,Coin,ApplyCoin,ApplyTime,AuditTime,AuditManagerID,AuditNote,FinanceID,PayTime,State,Note)");
			strSql.Append(" values (");
			strSql.Append("@Code,@InvestorID,@BankID,@BankCode,@Owner,@Coin,@ApplyCoin,@ApplyTime,@AuditTime,@AuditManagerID,@AuditNote,@FinanceID,@PayTime,@State,@Note)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,32),
					new SqlParameter("@InvestorID", SqlDbType.Int,4),
					new SqlParameter("@BankID", SqlDbType.Int,4),
					new SqlParameter("@BankCode", SqlDbType.VarChar,50),
					new SqlParameter("@Owner", SqlDbType.NVarChar,32),
					new SqlParameter("@Coin", SqlDbType.Decimal,9),
					new SqlParameter("@ApplyCoin", SqlDbType.Decimal,9),
					new SqlParameter("@ApplyTime", SqlDbType.VarChar,19),
					new SqlParameter("@AuditTime", SqlDbType.VarChar,19),
					new SqlParameter("@AuditManagerID", SqlDbType.Int,4),
					new SqlParameter("@AuditNote", SqlDbType.NText),
					new SqlParameter("@FinanceID", SqlDbType.Int,4),
					new SqlParameter("@PayTime", SqlDbType.VarChar,19),
					new SqlParameter("@State", SqlDbType.TinyInt,1),
					new SqlParameter("@Note", SqlDbType.NText)};
			parameters[0].Value = model.Code;
			parameters[1].Value = model.InvestorID;
			parameters[2].Value = model.BankID;
			parameters[3].Value = model.BankCode;
			parameters[4].Value = model.Owner;
			parameters[5].Value = model.Coin;
			parameters[6].Value = model.ApplyCoin;
			parameters[7].Value = model.ApplyTime;
			parameters[8].Value = model.AuditTime;
			parameters[9].Value = model.AuditManagerID;
			parameters[10].Value = model.AuditNote;
			parameters[11].Value = model.FinanceID;
			parameters[12].Value = model.PayTime;
			parameters[13].Value = model.State;
			parameters[14].Value = model.Note;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(ST_GetCoinApply model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update ST_GetCoinApply set ");
			strSql.Append("Code=@Code,");
			strSql.Append("InvestorID=@InvestorID,");
			strSql.Append("BankID=@BankID,");
			strSql.Append("BankCode=@BankCode,");
			strSql.Append("Owner=@Owner,");
			strSql.Append("Coin=@Coin,");
			strSql.Append("ApplyCoin=@ApplyCoin,");
			strSql.Append("ApplyTime=@ApplyTime,");
			strSql.Append("AuditTime=@AuditTime,");
			strSql.Append("AuditManagerID=@AuditManagerID,");
			strSql.Append("AuditNote=@AuditNote,");
			strSql.Append("FinanceID=@FinanceID,");
			strSql.Append("PayTime=@PayTime,");
			strSql.Append("State=@State,");
			strSql.Append("Note=@Note");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,32),
					new SqlParameter("@InvestorID", SqlDbType.Int,4),
					new SqlParameter("@BankID", SqlDbType.Int,4),
					new SqlParameter("@BankCode", SqlDbType.VarChar,50),
					new SqlParameter("@Owner", SqlDbType.NVarChar,32),
					new SqlParameter("@Coin", SqlDbType.Decimal,9),
					new SqlParameter("@ApplyCoin", SqlDbType.Decimal,9),
					new SqlParameter("@ApplyTime", SqlDbType.VarChar,19),
					new SqlParameter("@AuditTime", SqlDbType.VarChar,19),
					new SqlParameter("@AuditManagerID", SqlDbType.Int,4),
					new SqlParameter("@AuditNote", SqlDbType.NText),
					new SqlParameter("@FinanceID", SqlDbType.Int,4),
					new SqlParameter("@PayTime", SqlDbType.VarChar,19),
					new SqlParameter("@State", SqlDbType.TinyInt,1),
					new SqlParameter("@Note", SqlDbType.NText),
					new SqlParameter("@id", SqlDbType.Int,4)};
			parameters[0].Value = model.Code;
			parameters[1].Value = model.InvestorID;
			parameters[2].Value = model.BankID;
			parameters[3].Value = model.BankCode;
			parameters[4].Value = model.Owner;
			parameters[5].Value = model.Coin;
			parameters[6].Value = model.ApplyCoin;
			parameters[7].Value = model.ApplyTime;
			parameters[8].Value = model.AuditTime;
			parameters[9].Value = model.AuditManagerID;
			parameters[10].Value = model.AuditNote;
			parameters[11].Value = model.FinanceID;
			parameters[12].Value = model.PayTime;
			parameters[13].Value = model.State;
			parameters[14].Value = model.Note;
			parameters[15].Value = model.id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from ST_GetCoinApply ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from ST_GetCoinApply ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public ST_GetCoinApply GetModel(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 id,Code,InvestorID,BankID,BankCode,Owner,Coin,ApplyCoin,ApplyTime,AuditTime,AuditManagerID,AuditNote,FinanceID,PayTime,State,Note from ST_GetCoinApply ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			ST_GetCoinApply model=new ST_GetCoinApply();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public ST_GetCoinApply DataRowToModel(DataRow row)
		{
			ST_GetCoinApply model=new ST_GetCoinApply();
			if (row != null)
			{
				if(row["id"]!=null && row["id"].ToString()!="")
				{
					model.id=int.Parse(row["id"].ToString());
				}
				if(row["Code"]!=null)
				{
					model.Code=row["Code"].ToString();
				}
				if(row["InvestorID"]!=null && row["InvestorID"].ToString()!="")
				{
					model.InvestorID=int.Parse(row["InvestorID"].ToString());
				}
				if(row["BankID"]!=null && row["BankID"].ToString()!="")
				{
					model.BankID=int.Parse(row["BankID"].ToString());
				}
				if(row["BankCode"]!=null)
				{
					model.BankCode=row["BankCode"].ToString();
				}
				if(row["Owner"]!=null)
				{
					model.Owner=row["Owner"].ToString();
				}
				if(row["Coin"]!=null && row["Coin"].ToString()!="")
				{
					model.Coin=decimal.Parse(row["Coin"].ToString());
				}
				if(row["ApplyCoin"]!=null && row["ApplyCoin"].ToString()!="")
				{
					model.ApplyCoin=decimal.Parse(row["ApplyCoin"].ToString());
				}
				if(row["ApplyTime"]!=null)
				{
					model.ApplyTime=row["ApplyTime"].ToString();
				}
				if(row["AuditTime"]!=null)
				{
					model.AuditTime=row["AuditTime"].ToString();
				}
				if(row["AuditManagerID"]!=null && row["AuditManagerID"].ToString()!="")
				{
					model.AuditManagerID=int.Parse(row["AuditManagerID"].ToString());
				}
				if(row["AuditNote"]!=null)
				{
					model.AuditNote=row["AuditNote"].ToString();
				}
				if(row["FinanceID"]!=null && row["FinanceID"].ToString()!="")
				{
					model.FinanceID=int.Parse(row["FinanceID"].ToString());
				}
				if(row["PayTime"]!=null)
				{
					model.PayTime=row["PayTime"].ToString();
				}
				if(row["State"]!=null && row["State"].ToString()!="")
				{
					model.State=int.Parse(row["State"].ToString());
				}
				if(row["Note"]!=null)
				{
					model.Note=row["Note"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,Code,InvestorID,BankID,BankCode,Owner,Coin,ApplyCoin,ApplyTime,AuditTime,AuditManagerID,AuditNote,FinanceID,PayTime,State,Note ");
			strSql.Append(" FROM ST_GetCoinApply ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" id,Code,InvestorID,BankID,BankCode,Owner,Coin,ApplyCoin,ApplyTime,AuditTime,AuditManagerID,AuditNote,FinanceID,PayTime,State,Note ");
			strSql.Append(" FROM ST_GetCoinApply ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM ST_GetCoinApply ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from ST_GetCoinApply T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}


		#endregion  BasicMethod
		#region  ExtensionMethod
       /// <summary>
       /// 提现记录Code
       /// </summary>
       /// <param name="Title"></param>
       /// <param name="ID"></param>
       /// <param name="length"></param>
       /// <returns></returns>
        public string GetCode(string Title, int ID, int length)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT [dbo].[GetCoinApplyCode](@Title,@ID,@length) ");
            SqlParameter[] parameters = {
					new SqlParameter("@Title", SqlDbType.VarChar,10),
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@length", SqlDbType.Int,4)};
            parameters[0].Value = Title;
            parameters[1].Value = ID;
            parameters[2].Value = length;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return "";
            }
            else
            {
                return obj.ToString();
            }
        }
       /// <summary>
       /// 修改提现申请code
       /// </summary>
       /// <param name="code"></param>
       /// <param name="id"></param>
       /// <returns></returns>
        public bool Update(string code, int id)
        { 
            StringBuilder strSql=new StringBuilder();
			strSql.Append("update ST_GetCoinApply set ");
			strSql.Append("Code=@Code ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,32),
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = code;
			parameters[1].Value = id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
        }
		#endregion  ExtensionMethod
	}
}